﻿using System;
using System.Application;
using System.Domain.Entity;
using Microsoft.AspNetCore.Mvc;
using MySqlX.XDevAPI;
using NLog;
using Ubiquity.Core.Extend;
using Ubiquity.Core.Web;

namespace Ubiquity.Web.Controllers
{
    public class LoginController : Controller
    {
        [HttpGet]
        public virtual ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult GetAuthCode()
        {
            return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult OutLogin()
        {
            //SysOperationLogEntity logEntity = new SysOperationLogEntity
            //{ 
            //    Type = OperationLogType.Login.ToString(),
            //    IpAddress = NetHelper.Ip,
            //    Browser = System.Web.HttpContext.Current.Request.Browser.Browser,
            //    Version = System.Web.HttpContext.Current.Request.Browser.Version,
            //    Url = System.Web.HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"],//页面
            //    UserAgent = System.Web.HttpContext.Current.Request.UserAgent,
            //    RequestType = System.Web.HttpContext.Current.Request.RequestType,
            //    OperationCon = "安全退出系统"
            //}; 
            //OperatorModel operatorModel = OperatorProvider.Provider.GetCurrently(LoginManage.GetLoginId());
            //logEntity.Result = true;
            //new SysOperationLogService(LoginManage.GetLoginId()).WriteDbLog(logEntity);
            //if (operatorModel != null)
            //{
            //    OperatorProvider.Provider.RemoveCurrently(LoginManage.GetLoginId());
            //}
            //Session.Abandon();
            //Session.Clear();
            //OperatorProvider.Provider.RemoveCurrently(LoginManage.GetLoginId());
            return RedirectToAction("Index", "Login");
        }
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost] 
        public IActionResult CheckLogin(string username, string password, string code)
        {
            //SysOperationLogEntity logEntity = new SysOperationLogEntity
            //{

            //    Type = OperationLogType.Login.ToString(),
            //    IpAddress = NetHelper.Ip,
            //    Browser = System.Web.HttpContext.Current.Request.Browser.Browser,
            //    Version = System.Web.HttpContext.Current.Request.Browser.Version,
            //    Url = System.Web.HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"],//页面
            //    UserAgent = System.Web.HttpContext.Current.Request.UserAgent,
            //    RequestType = System.Web.HttpContext.Current.Request.RequestType,
            //    OperationCon = "系统登录"
            //};
            //try
            //{
            //    if (Session["session_verifycode"].IsEmpty() ||
            //        Md5.md5(code.ToLower(), 16) != Session["session_verifycode"].ToString())
            //    {
            //        string errestr = "验证码错误，请重新输入";
            //        logEntity.Result = false;
            //        logEntity.Description = "用户名:" + username + ";登录失败，" + errestr;
            //        new SysOperationLogService("").WriteDbLog(logEntity);
            //        return Content(new AjaxResult { state = ResultType.error.ToString(), message = errestr }.ToJson());
            //    }
            //    ISysUserService userService = new SysUserService("");
            //    SysUserEntity userEntity = userService.Login(username, password);

            //    //验证信息 登陆

            //    if (userEntity != null)
            //    {
            //        OperatorModel operatorModel = new OperatorModel
            //        {
            //            UserId = userEntity.Id,
            //            UserCode = userEntity.Account,
            //            UserName = userEntity.RealName,
            //            CompanyId = userEntity.OrganizeId,
            //            DepartmentId = userEntity.DepartmentId,
            //            RoleId = userEntity.RoleId
            //            //LoginIpAddress = NetHelper.Ip
            //        };

            //        ////如果是admin 赋值管理员权限
            //        //operatorModel.IsSystem = userEntity.Account == "admin";
            //        string loginId = Common.GetTimeStamp();
            //        if (LoginManage.AddLoginId(loginId))
            //        {
            //            OperatorProvider.Provider.AddCurrently(loginId, operatorModel);
            //        }

            //        logEntity.Result = true;
            //        logEntity.Description = "登录成功";
            //        new SysOperationLogService(LoginManage.GetLoginId()).WriteDbLog(logEntity);
            //        string dataUrl = "/Home/Index";
            //        //  if (userEntity.IsPay != null && (bool)userEntity.IsPay)
            //        {
            //            //  dataUrl = "/StorePayment/StoreHome/Index";

            //        }
            //        //SysOperationLogEntity logEntity1=null ;

            //        //logEntity1.Browser = "1";

            //        return Content(new AjaxResult { state = ResultType.success.ToString(), message = "登录成功。", data = dataUrl }.ToJson());
            //    }
            //    else
            //    {
            //        logEntity.Result = false;
            //        logEntity.Description = "用户名:" + username + ";登录失败，用户或密码错误";
            //        new SysOperationLogService("").WriteDbLog(logEntity);
            //        return Content(new AjaxResult { state = ResultType.error.ToString(), message = "登录失败,用户或密码错误", }.ToJson());
            //    }

            //}
            //catch (Exception ex)
            //{
            //    logEntity.Result = false;
            //    logEntity.Description = "登录失败，" + ex.Message;
            //    //   new SysOperationLogService("").WriteDbLog(logEntity);
            //    LogFactory.GetLogger(GetType().ToString()).Error(ex);
            //    return Content(new AjaxResult { state = ResultType.error.ToString(), message = ex.Message }.ToJson());
            //}
            return Content("");
        }


    }
}
